package com.example.plugins.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * ClassName:RabbitConfig
 * Package:com.example.delay1.config
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/8/13-20:15
 * @Version:v1.0
 */
@Configuration
public class RabbitConfig {

    @Value("${my.exchangeName}")
    private String exchangeName;

    @Value("${my.queueDelayName}")
    private String queueDelayName;


    //交换机类型
    private static final String EXCHANGE_TYPE = "x-delayed-type";
    //直连交换机
    private static final String DIRECT_EXCHANGE = "direct";

    /**
     * 创建自定义交换机
     * @return
     */
    @Bean
    public CustomExchange customExchange() {
        /**
         * CustomExchange(String name, String type, boolean durable, boolean autoDelete, Map<String, Object> arguments)
         * 1.交换机名称
         * 2.交换机类型
         * 3.是否持久化
         * 4.自动删除
         * 5.参数
         */
        Map<String, Object> arguments = new HashMap<>();
        arguments.put(EXCHANGE_TYPE, DIRECT_EXCHANGE);//放一个参数

        return new CustomExchange(exchangeName, "x-delayed-message", true, false, arguments);

    }

    @Bean
    public Queue queue() {

        return QueueBuilder.durable(queueDelayName).build();

    }




    @Bean
    public Binding binding(CustomExchange customExchange, Queue queue) {
        //绑定，也要指定路由key，加没有其他参数
        return BindingBuilder.bind(queue).to(customExchange).with("plugin").noargs();
    }


}
